<?php

namespace app\api\controller\admin;

use app\common\controller\TApi;
use think\Db;
use think\Collection;
/**
 * 家长端接口
 */
class Config extends TApi
{
    protected $noNeedLogin = ['*'];

    //选择学生
    public function selectStudent() {

        $classesId = $this->request->get('classesId');
        $query1 = $classesId ? ['ds.classes_id' => $classesId] : [];
        $username = $this->request->get('username');
        $query2 = $username ? ['ds.username' => ['LIKE', '%' . $username . '%']] : [];

        $list = Db::name('dts_student')->alias('ds')
        ->field('ds.id as studentId,ds.username,ds.gender,ds.birthday,ds.relationship,dc.id as classesId,dc.name as classname')
        ->join('dts_classes dc', 'dc.id = ds.classes_id')
        ->where(array_merge($query1, $query2))
        ->select();

        $map = new Collection;

        foreach($list as $item) {
            $key = $item['classesId'];
            if ($map->offsetExists($key)) {
                $obj = $map->offsetGet($key);
                $obj['children'][] = ['id' => $item['studentId'], 'username' => $item['username'], 'gender' => $item['gender'], 'birthday' => $item['birthday'], 'relationship' => $item['relationship']];
                $map[$key] = $obj;
            }else {
                $data = ['id' => $item['classesId'], 'classname' => $item['classname'], 'children' => []];
                $data['children'][] = ['id' => $item['studentId'], 'username' => $item['username'], 'gender' => $item['gender'], 'birthday' => $item['birthday'], 'relationship' => $item['relationship']];

                $map[$key] = $data;
            }
        }

        $this->success('success', array_values($map->toArray()));
    }

    //选择职工
    public function selectAdmin() {
        
        $deptId = $this->request->get('deptId');
        $query1 = $deptId ? ['ad.dept_id' => $deptId] : [];
        $nickname = $this->request->get('nickname');
        $query2 = $nickname ? ['ad.nickname' => ['LIKE', '%' . $nickname . '%']] : [];
        $isrepair = $this->request->get('isrepair');
        $query3 = $isrepair ? ['ad.is_repair' => 1] : [];

        $list = Db::name('admin')->alias('ad')
        ->field('ad.id as adminId,ad.avatar,ad.nickname,dd.id as deptId,dd.name as deptname')
        ->join('dts_dept dd', 'dd.id = ad.dept_id')
        ->where(array_merge($query1, $query2, $query3))
        ->select();

        $map = new Collection;

        foreach($list as $item) {
            $key = $item['deptId'];
            if ($map->offsetExists($key)) {
                $obj = $map->offsetGet($key);
                $obj['children'][] = ['id' => $item['adminId'], 'avatar' => $item['avatar'], 'nickname' => $item['nickname']];
                $map[$key] = $obj;
            }else {
                $data = ['id' => $item['deptId'], 'deptname' => $item['deptname'], 'children' => []];
                $data['children'][] = ['id' => $item['adminId'], 'avatar' => $item['avatar'], 'nickname' => $item['nickname']];

                $map[$key] = $data;
            }
        }

        $this->success('success', array_values($map->toArray()));
    }

    //选择部门
    public function selectDept() {
        
        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_dept')->field('id,name')->where($query)->select();

        $this->success('success', $list);
    }

    //选择年级
    public function selectGrade() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_grade')->field('id,name')->where($query)->select();

        $this->success('success', $list);
    }

    //选择班级
    public function selectClass() {

        $name = $this->request->get('name');
        $query1 = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];
        $gradeId = $this->request->get('gradeId');
        $query2 = $gradeId ? ['grade_id' => $gradeId] : [];

        $list = Db::name('dts_classes')->field('id,name')->where(array_merge($query1, $query2))->select();

        $this->success('success', $list);
    }

    //选择活动邻域
    public function selectRealm() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_activity_realm')->field('id,name')->where($query)->select();

        $this->success('success', $list);
    }

    //选择活动标签
    public function selectLabel() {
        
        $realmId = $this->request->get('realmId');
        $query1 = $realmId ? ['dts_activity_realm_id' => $realmId] : [];
        $name = $this->request->get('name');
        $query2 = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_activity_label')->field('id,name')->where(array_merge($query1, $query2))->select();

        $this->success('success', $list);
    }

    //选择物资
    public function selectMaterial() {

        $category = $this->request->get('category');
        if(!$category) {
            $this->error('物资类型有误');
        }

        $name = $this->request->get('name');
        $query = $name ? ['material.name' => ['LIKE', '%' . $name . '%']] : [];
        $numType = $this->request->get('numType');
        $queryRaw = $numType ? '(material.quantity - material.consume) > 0' : '1=1';

        $list = Db::name('dts_material_'.$category)->alias('material')
        ->field('material.id as materialId,material.name as materialName,material.images,material.specs,material.unity,material.quantity,material.consume,category.id as categoryId,category.name as categoryname')
        ->join([
            ['dts_material_category category', 'category.id = material.material_category_id', 'left']
        ])
        ->where($query)
        ->whereRaw($queryRaw)
        ->select();

        $map = new Collection;

        foreach($list as $item) {
            $key = $item['categoryId'];
            if ($map->offsetExists($key)) {
                $obj = $map->offsetGet($key);
                $obj['children'][] = ['id' => $item['materialId'], 'name' => $item['materialName'], 'images' => $item['images'], 'specs' => $item['specs'], 'unity' => $item['unity'], 'quantity' => $item['quantity'], 'consume' => $item['consume']];
                $map[$key] = $obj;
            }else {
                $data = ['id' => $item['categoryId'], 'name' => $item['categoryname'], 'children' => []];
                $data['children'][] = ['id' => $item['materialId'], 'name' => $item['materialName'], 'images' => $item['images'], 'specs' => $item['specs'], 'unity' => $item['unity'], 'quantity' => $item['quantity'], 'consume' => $item['consume']];

                $map[$key] = $data;
            }
        }

        $this->success('success', array_values($map->toArray()));
    }

    //选择请假类型
    public function selectAskfor() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_askfor')->where($query)->select();

        $this->success('success', $list);
    }

    //选择公车
    public function selectBus() {

        $name = $this->request->get('name');
        $query = $name ? ['bus.name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_bus')->alias('bus')
        ->field('bus.*,admin.nickname as adminfame')
        ->join('admin admin', 'admin.id = bus.admin_id', 'left')
        ->where($query)
        ->select();

        $this->success('success', $list);
    }

    //选择二维码
    public function selectBarcode() {

        $identifier = $this->request->get('identifier');
        $query = $identifier ? ['identifier' => ['LIKE', '%' . $identifier . '%']] : [];

        $list = Db::name('dts_config_barcode')->where($query)->where('status', 0)->select();

        $this->success('success', $list);
    }

    //选择资金项目
    public function selectCapital() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_capital')->where($query)->select();

        $this->success('success', $list);
    }

    //选择文件类
    public function selectFiles() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_files')->where($query)->select();

        $this->success('success', $list);
    }

    //选择功能室
    public function sleectFunctional() {

        $name = $this->request->get('name');
        $query = $name ? ['dcf.name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_functional')->alias('dcf')
        ->field('dcf.id,dcf.name,dcf.examine_id,ad.nickname as examinename')
        ->join('admin ad', 'ad.id = dcf.examine_id', 'left')
        ->where($query)
        ->select();

        $this->success('success', $list);
    }

    //选择会议室
    public function selectMeeting() {
        
        $name = $this->request->get('name');
        $query = $name ? ['dcm.name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_meeting')->alias('dcm')
        ->field('dcm.id,dcm.name,dcm.examine_id,dcm.copy_ids,ad.nickname as examinename,GROUP_CONCAT(DISTINCT copy.nickname) as copyname')
        ->join([
            ['admin ad', 'ad.id = dcm.examine_id', 'left'],
            ['admin copy', 'FIND_IN_SET(copy.id,dcm.copy_ids)', 'left']
        ])
        ->where($query)
        ->group('dcm.id')
        ->select();

        $this->success('success', $list);
    }

    //选择生产者
    public function selectProduce() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_produce')->where($query)->select();

        $this->success('success', $list);
    }

    //选择印章
    public function selectSeal() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_seal')->where($query)->select();

        $this->success('success', $list);
    }

    //选择供应商
    public function selectSupplier() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_supplier')->where($query)->select();

        $this->success('success', $list);
    }

    //选择资源库
    public function selectClassify() {
        $queryRaw = '1=1';
        $admin = $this->auth->getAdmininfo();
        if($admin) {
            $queryRaw = 'FIND_IN_SET('.$admin['id'].',label.visible_admin_ids) > 0 OR label.visible_admin_ids is NULL';
        }
        $name = $this->request->get('name');
        $query = $name ? ['label.name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_classify_type')->alias('type')
        ->field('type.id as typeId, type.name as typeName,label.id as labelId,label.name as labelName')
        ->join('dts_config_classify_label label', 'label.config_classify_type_id = type.id', 'left')
        ->where($query)
        ->whereRaw($queryRaw)
        ->select();

        if(!$list) {
            $list = Db::name('dts_config_classify_type')->field('id as typeId, name as typeName')->select();
        }

        $map = new Collection;

        foreach($list as $item) {
            $key = $item['typeId'];
            if ($map->offsetExists($key)) {
                $obj = $map->offsetGet($key);
                $obj['children'][] = ['id' => $item['labelId'], 'name' => $item['labelName']];
                $map[$key] = $obj;
            }else {
                $data = ['id' => $item['typeId'], 'name' => $item['typeName'], 'children' => []];
                if(isset($item['labelId']) && isset($item['labelName'])) {
                    $data['children'][] = ['id' => $item['labelId'], 'name' => $item['labelName']];
                }

                $map[$key] = $data;
            }
        }

        $this->success('success', array_values($map->toArray()));
    }

    //选择资源库详情
    public function selectClassifyInfo() {

        $labelId = $this->request->get('labelId');

        $info = Db::name('dts_config_classify_label')->alias('label')
        ->field('label.*,type.name as typeName,GROUP_CONCAT(visible.nickname) as visible_admin_names')
        ->join('dts_config_classify_type type', 'type.id = label.config_classify_type_id', 'left')
        ->join('admin visible', 'FIND_IN_SET(visible.id,label.visible_admin_ids)', 'left')
        ->where('label.id', $labelId)
        ->find();

        $this->success('success', $info);
    }

    public function selectRefuel() {

        $name = $this->request->get('name');
        $query = $name ? ['name' => ['LIKE', '%' . $name . '%']] : [];

        $list = Db::name('dts_config_refuel')->where($query)->select();

        $this->success('success', $list);
    }
}